package cn.lcsjsxy.yxz.day.IO;

import java.io.*;

//序列化
public class Serialization {
    public static void main(String[] args) {

        //文件复制~序列化 / 反序列化

        // TODO 数据文件对象
        File datFile = new File("D:\\yang\\idea\\day10\\data\\obj.dat");

        //对象输出流(管道对象)
        ObjectOutputStream objectOut = null;
        FileOutputStream out = null;
        //输入
        ObjectInputStream objectIn = null;
        FileInputStream in = null;

        try {
            out = new FileOutputStream(datFile);
            objectOut = new ObjectOutputStream(out);

            // Java中只有增加了特殊的标记的类，才能再写文件中时进行序列化操作
            // 这里的标记其实就是一个按口
            User user = new User();
            objectOut.writeObject(user);
            objectOut.flush();

            //从文件中读取数据转换成对象
            in = new FileInputStream(datFile);
            objectIn = new ObjectInputStream(in);
            Object o = objectIn.readObject();
            System.out.println(o);

        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if (objectOut != null){
                try {
                    objectOut.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

class User implements Serializable{

}